package frames;

import classes.Categoria;
import classes.Conecta;
import classes.Produto;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;


public class ListarProdut extends javax.swing.JFrame {
    GerenciaCategoria gerenciaCategoria = new GerenciaCategoria();
    Categoria categoria = new Categoria();
    List<Produto> listaProd = new ArrayList<Produto>();
    Produto produto = new Produto();
    Conecta conecta = new Conecta();
    DefaultComboBoxModel modeloCombo = new DefaultComboBoxModel();
    DefaultTableModel model = new DefaultTableModel(null, new String[]{"Produto", "Código de Barras", "Categoria", "Quantidade", "Valor venda", "Valor compra"});
    ListSelectionModel lsProdutos;

    /** Creates new form ListarProdut */
    public ListarProdut() {
        initComponents();
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField5 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jFormattedTextField3 = new javax.swing.JFormattedTextField();
        jLabel6 = new javax.swing.JLabel();
        panelFiltro = new javax.swing.JPanel();
        lbProduto = new javax.swing.JLabel();
        lbBarCode = new javax.swing.JLabel();
        lbCategoria = new javax.swing.JLabel();
        lbQuantidade = new javax.swing.JLabel();
        tfProduto = new javax.swing.JTextField();
        tfBarCode = new javax.swing.JTextField();
        tfQuantidade = new javax.swing.JTextField();
        lbCompra = new javax.swing.JLabel();
        lbVenda = new javax.swing.JLabel();
        ftfCompra = new javax.swing.JFormattedTextField();
        ftfVenda = new javax.swing.JFormattedTextField();
        carregaCB(conecta.listaTodasCategorias());
        cbCategoria = new javax.swing.JComboBox();
        lbId = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        tfId = new javax.swing.JTextField();
        panelLista = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableLista = new javax.swing.JTable();
        mostrarPesquisa(conecta.listaTodosProdutos());
        panelBotao = new javax.swing.JPanel();
        btVoltar = new javax.swing.JButton();
        btLimpar = new javax.swing.JButton();
        btListar = new javax.swing.JButton();
        btAlterar = new javax.swing.JButton();

        jTextField5.setText("jTextField5");

        jLabel3.setText("jLabel3");

        jFormattedTextField3.setText("jFormattedTextField3");

        jLabel6.setText("jLabel6");

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                Confirma(evt);
            }
        });

        panelFiltro.setBorder(javax.swing.BorderFactory.createTitledBorder("Filtro"));

        lbProduto.setText("Produto:");

        lbBarCode.setText("Código de Barras:");

        lbCategoria.setText("Categoria:");

        lbQuantidade.setText("Quantidade:");

        lbCompra.setText("Valor Compra:");

        lbVenda.setText("Valor Venda:");

        ftfCompra.setEditable(false);
        ftfCompra.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));
        ftfCompra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ftfCompraActionPerformed(evt);
            }
        });

        ftfVenda.setEditable(false);
        ftfVenda.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0.00"))));

        cbCategoria.setModel(modeloCombo);
        cbCategoria.setSelectedIndex(-1);
        cbCategoria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbCategoriaActionPerformed(evt);
            }
        });

        lbId.setText("ID:");

        tfId.setEditable(false);

        javax.swing.GroupLayout panelFiltroLayout = new javax.swing.GroupLayout(panelFiltro);
        panelFiltro.setLayout(panelFiltroLayout);
        panelFiltroLayout.setHorizontalGroup(
            panelFiltroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelFiltroLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelFiltroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelFiltroLayout.createSequentialGroup()
                        .addComponent(lbId, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfId, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(lbBarCode))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelFiltroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelFiltroLayout.createSequentialGroup()
                        .addComponent(tfBarCode, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbCategoria)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                        .addComponent(cbCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(panelFiltroLayout.createSequentialGroup()
                        .addComponent(lbProduto)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tfProduto, javax.swing.GroupLayout.PREFERRED_SIZE, 277, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbQuantidade)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfQuantidade, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(panelFiltroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelFiltroLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panelFiltroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(panelFiltroLayout.createSequentialGroup()
                                .addComponent(lbVenda)
                                .addGap(18, 18, 18)
                                .addComponent(ftfVenda, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())))
                    .addGroup(panelFiltroLayout.createSequentialGroup()
                        .addComponent(lbCompra)
                        .addGap(18, 18, 18)
                        .addComponent(ftfCompra, javax.swing.GroupLayout.DEFAULT_SIZE, 82, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        panelFiltroLayout.setVerticalGroup(
            panelFiltroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelFiltroLayout.createSequentialGroup()
                .addGroup(panelFiltroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelFiltroLayout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(panelFiltroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(panelFiltroLayout.createSequentialGroup()
                                .addGroup(panelFiltroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(lbQuantidade)
                                    .addComponent(tfQuantidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cbCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(panelFiltroLayout.createSequentialGroup()
                                .addGroup(panelFiltroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(lbId, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(lbProduto)
                                    .addComponent(tfProduto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(tfId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(panelFiltroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(lbBarCode)
                                    .addComponent(tfBarCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lbCategoria)))))
                    .addGroup(panelFiltroLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panelFiltroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lbVenda)
                            .addComponent(ftfVenda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panelFiltroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lbCompra)
                            .addComponent(ftfCompra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(22, 22, 22)))
                .addGap(31, 31, 31))
        );

        panelLista.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista"));

        tableLista.setAutoCreateRowSorter(true);
        tableLista.setModel(model);
        tableLista.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tableLista.setUpdateSelectionOnSort(false);
        tableLista.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        lsProdutos = tableLista.getSelectionModel();
        lsProdutos.addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent e){
                if(e.getValueIsAdjusting()){
                    tabelaLinhaSelecionada(tableLista.getSelectedRow());
                }
            }
        });
        jScrollPane1.setViewportView(tableLista);

        javax.swing.GroupLayout panelListaLayout = new javax.swing.GroupLayout(panelLista);
        panelLista.setLayout(panelListaLayout);
        panelListaLayout.setHorizontalGroup(
            panelListaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelListaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 792, Short.MAX_VALUE)
                .addContainerGap())
        );
        panelListaLayout.setVerticalGroup(
            panelListaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelListaLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 357, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(56, 56, 56))
        );

        btVoltar.setText("Voltar");
        btVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btVoltarActionPerformed(evt);
            }
        });

        btLimpar.setText("Limpar");
        btLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLimparActionPerformed(evt);
            }
        });

        btListar.setText("Listar");
        btListar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btListarActionPerformed(evt);
            }
        });

        btAlterar.setText("Alterar");
        btAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAlterarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelBotaoLayout = new javax.swing.GroupLayout(panelBotao);
        panelBotao.setLayout(panelBotaoLayout);
        panelBotaoLayout.setHorizontalGroup(
            panelBotaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelBotaoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btAlterar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btListar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btLimpar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btVoltar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        panelBotaoLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btAlterar, btLimpar, btListar, btVoltar});

        panelBotaoLayout.setVerticalGroup(
            panelBotaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelBotaoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelBotaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btAlterar)
                    .addComponent(btVoltar)
                    .addComponent(btLimpar)
                    .addComponent(btListar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelFiltro, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelLista, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelBotao, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(panelFiltro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelLista, javax.swing.GroupLayout.PREFERRED_SIZE, 404, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelBotao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btVoltarActionPerformed
        int resposta = JOptionPane.showConfirmDialog(null, "Deseja finalizar essa operação?", "Finalizar", JOptionPane.YES_NO_OPTION);
        //sim = 0, nao = 1  
        if (resposta == 0) {
            new MenuOpcoes().setVisible(true);
            dispose();
        }
    }//GEN-LAST:event_btVoltarActionPerformed

    private void btLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLimparActionPerformed
        limparTudo();
        mostrarPesquisa(listaProd);

    }//GEN-LAST:event_btLimparActionPerformed

    private void btListarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btListarActionPerformed
        if (tfBarCode.getText().isEmpty() && cbCategoria.getSelectedIndex() == -1 && tfProduto.getText().isEmpty() && tfQuantidade.getText().isEmpty()) {
            listaProd = conecta.listaTodosProdutos();
        } else {
            if (!tfBarCode.getText().isEmpty()) {
                listaProd = conecta.listaProduto("%" + tfBarCode.getText() + "%", 4);
            } else if (cbCategoria.getSelectedIndex() != -1) {
                listaProd = conecta.listaProduto("%" + cbCategoria.getSelectedItem() + "%", 3);
            } else if (!tfProduto.getText().isEmpty()) {
                listaProd = conecta.listaProduto("%" + tfProduto.getText() + "%", 1);
            } else if (!tfQuantidade.getText().isEmpty()) {
                listaProd = conecta.listaProduto("%" + tfQuantidade.getText() + "%", 2);
            }
        }
        mostrarPesquisa(listaProd);
    }//GEN-LAST:event_btListarActionPerformed

    private void Confirma(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_Confirma
        int resposta = JOptionPane.showConfirmDialog(null, "Deseja finalizar essa operação?", "Finalizar", JOptionPane.YES_NO_OPTION);
        //sim = 0, nao = 1  
        if (resposta == 0) {
            System.exit(0);
        }
    }//GEN-LAST:event_Confirma

    private void cbCategoriaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbCategoriaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbCategoriaActionPerformed

    private void btAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAlterarActionPerformed
        produto.setId(Integer.parseInt(tfId.getText()));
        produto.setBarCode(tfBarCode.getText());
        produto.setCompra(Float.parseFloat(ftfCompra.getText().replace(",", ".")));
        produto.setNomeProduto(tfProduto.getText());
        produto.setQuantidade(Integer.parseInt(tfQuantidade.getText()));
        produto.setVenda(Float.parseFloat(ftfVenda.getText().replace(",", ".")));
        produto.setCategoria(cbCategoria.getSelectedItem().toString());
        conecta.alteraProduto(produto);
        limparTudo();
    }//GEN-LAST:event_btAlterarActionPerformed

    private void ftfCompraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ftfCompraActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ftfCompraActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ListarProdut.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ListarProdut.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ListarProdut.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ListarProdut.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ListarProdut().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAlterar;
    private javax.swing.JButton btLimpar;
    private javax.swing.JButton btListar;
    private javax.swing.JButton btVoltar;
    private javax.swing.JComboBox cbCategoria;
    private javax.swing.JFormattedTextField ftfCompra;
    private javax.swing.JFormattedTextField ftfVenda;
    private javax.swing.JFormattedTextField jFormattedTextField3;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JLabel lbBarCode;
    private javax.swing.JLabel lbCategoria;
    private javax.swing.JLabel lbCompra;
    private javax.swing.JLabel lbId;
    private javax.swing.JLabel lbProduto;
    private javax.swing.JLabel lbQuantidade;
    private javax.swing.JLabel lbVenda;
    private javax.swing.JPanel panelBotao;
    private javax.swing.JPanel panelFiltro;
    private javax.swing.JPanel panelLista;
    private javax.swing.JTable tableLista;
    private javax.swing.JTextField tfBarCode;
    private javax.swing.JTextField tfId;
    private javax.swing.JTextField tfProduto;
    private javax.swing.JTextField tfQuantidade;
    // End of variables declaration//GEN-END:variables

    private void mostrarPesquisa(List<Produto> lista) {
        this.listaProd = lista;
        while (model.getRowCount() > 0) {
            model.removeRow(0);
        }
        if (lista.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Valor não encontrado");
        } else {
            for (int i = 0; i < lista.size(); i++) {
                String[] linha = {null, null, null, null, null, null};
                model.addRow(linha);
                model.setValueAt(lista.get(i).getNomeProduto(), i, 0);
                model.setValueAt(lista.get(i).getBarCode(), i, 1);
                model.setValueAt(lista.get(i).getCategoria(), i, 2);
                model.setValueAt(lista.get(i).getQuantidade(), i, 3);
                model.setValueAt(lista.get(i).getVenda(), i, 4);
                model.setValueAt(lista.get(i).getCompra(), i, 5);
            }
        }

    }

    private void tabelaLinhaSelecionada(int i) {
        ftfCompra.setEditable(true);
        ftfVenda.setEditable(true);
        tfId.setText(String.valueOf(listaProd.get(i).getId()));
        tfProduto.setText(listaProd.get(i).getNomeProduto());
        tfBarCode.setText(listaProd.get(i).getBarCode());
        cbCategoria.setSelectedItem(listaProd.get(i).getCategoria());
        tfQuantidade.setText(String.valueOf(listaProd.get(i).getQuantidade()));
        ftfCompra.setText(String.valueOf(listaProd.get(i).getCompra()));
        ftfVenda.setText(String.valueOf(listaProd.get(i).getVenda()));

    }

    private void limparTudo() {
        tfId.setText(null);
        ftfCompra.setText(null);
        ftfVenda.setText(null);
        tfBarCode.setText(null);
        cbCategoria.setSelectedIndex(-1);
        tfProduto.setText(null);
        tfQuantidade.setText(null);
        while (model.getRowCount() > 0) {
            model.removeRow(0);
        }
        listaProd = conecta.listaTodosProdutos();
        mostrarPesquisa(listaProd);
    }
    
    private void carregaCB(List<Categoria> listaCategoria){
            if (listaCategoria.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Sem categorias cadastras");
        } else {
            modeloCombo.removeAllElements();
            for (int i = 0; i < listaCategoria.size(); i++) {
                modeloCombo.addElement(listaCategoria.get(i).getCategoria());
            }
        }

    }

}
